package org.zk.dubbo_consumer.controller;


import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Course;
import org.zk.entity.Subject;
import org.zk.entity.Type;
import org.zk.service.CourseService;
import org.zk.service.SubjectService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 科目表 前端控制器
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Resource
    private SubjectService subjectService;
    @Resource
    @Getter
    @Setter
    private CourseService courseService;

    @RequestMapping("/getSubject")
    public List getSubject(int typeId){
        return subjectService.getSubjectByTid(typeId);
    }


    @RequestMapping("/toSubject")
    public ModelAndView tosubject(int id){
        ModelAndView mv = new ModelAndView("admin/classify/subject");
        mv.addObject("typeId", id);
        return mv;

    }

    @RequestMapping("/addSubject")
    public ModelAndView addSubject(int typeId){
        ModelAndView mv = new ModelAndView("admin/classify/addSubject");
        mv.addObject("tid", typeId);
        return mv;

    }

    @RequestMapping("/getAllSubjectPages")
    @ResponseBody
    public Map getAllSbuject(String pageSize,String pageNum,String id){
        Map map=new HashMap();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
        map.put("typeId",Integer.parseInt(id));
        PageInfo page=subjectService.getAllSubjectsPage(map);
        map.put("page",page);
        map.put("pages",page.getPages());
        map.put("total",page.getTotal());
        return map;

    }

    @RequestMapping("/insertSubject")
    @ResponseBody
    public int insertSubject(String subjectName, int id){
        Map map=new HashMap();
        if(subjectName.equals("")){
            return 0;
        }else {
            map.put("subjectName",subjectName);
            map.put("id",id);
            subjectService.insertSubject(map);
            return 1;
        }


    }
    @RequestMapping("/delSubjectCourseBySid")
    @ResponseBody
    public int delSubjectCourseBySid(int id){
   List<Course> courseList= courseService.getCourseBySid(id);
       if(courseList.size()==0){
           return 0;
       }
        int result=subjectService.delSubjectCourseBySid(id);
        return result;
    }

    @RequestMapping("/getSubjectById")
    public ModelAndView toUpdateType(int id){
        ModelAndView mv=new ModelAndView("admin/classify/updateSubject");
        String name= subjectService.getSubjectByid(id);
        mv.addObject("name",name);
        mv.addObject("subjectId",id);
        return mv;
    }

    @RequestMapping("/updateSubject")
    @ResponseBody
    public int uoType(Subject subject){
        Map map1=new HashMap();
        map1.put("subject",subject);
        int result=subjectService.updateSubjectByTid(map1);
        return result;
    }

    /**
     * 获取二级标题
     * @param typeId
     * @return
     */
    @RequestMapping("/getSubjectByTypeId")
    public List getSubjectByTypeId(Integer typeId){
        Map map=new HashMap();
        map.put("typeId",typeId);
        return subjectService.getSubjectByTypeId(map);
    }

    public SubjectService getSubjectService() {
        return subjectService;
    }

    public void setSubjectService(SubjectService subjectService) {
        this.subjectService = subjectService;
    }
}

